package com.example.myapplication.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.adapter.DailyMenusAdapter;
import com.example.myapplication.R;
import com.example.myapplication.activity.MenuTendingMainActivity2;
import com.example.myapplication.adapter.MennuAdapter;
import com.example.myapplication.food_message.Menu;
import com.example.myapplication.kit.MessageEvent;
import com.example.myapplication.kit.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;


public class HomeRightFragment1 extends Fragment implements View.OnClickListener {
    private MennuAdapter myAdapter;
    private View view;
    private Context mContext;
    private GridView gv;
    private ArrayList<Menu> lists = null;
    private DailyMenusAdapter dailyMenusAdapter = null;
    private boolean canInputCash = false;
    //点击进行功能切换的按钮
    private LinearLayout one_priced, two_priced;
    //定价菜切换的的TXT文本
    private TextView tx_priced;
    //总金额
    private String inputCash = "";
    //金额展示框
    private EditText mETCash;
    //按键1-9，    三个功能键 确定，小数点，清除
    Button mTVNum1, mTVNum2, mTVNum3, mTVNum4, mTVNum5, mTVNum6, mTVNum7, mTVNum8, mTVNum9, mTVNum0, mTVNumSure, mTVNumCircle, mTVNumClear;
    //展示页面一（菜品展示），页面2（定价菜）
    private LinearLayout linearLayout1, linearLayout2, linearLayout3, linearLayout4, linearLayout5;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void setListData(ArrayList<Menu> data) {
        this.lists = data;
    }

    //id获取计时事件的操作
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_right1, container, false);
        gv = view.findViewById(R.id.gv_home_show);
        one_priced = view.findViewById(R.id.main_setting_priced);
        two_priced = view.findViewById(R.id.main_setting_maintain);
        tx_priced = view.findViewById(R.id.main_setting_priced_txt);
        one_priced.setOnClickListener(this);
        two_priced.setOnClickListener(this);
        linearLayout1 = view.findViewById(R.id.home_forgment_down_one);
        linearLayout2 = view.findViewById(R.id.home_forgment_down_two);
        mETCash = view.findViewById(R.id.tv_code_mark);
        mTVNum0 = view.findViewById(R.id.tx_num0);
        mTVNum1 = view.findViewById(R.id.tx_num1);
        mTVNum2 = view.findViewById(R.id.tx_num2);
        mTVNum3 = view.findViewById(R.id.tx_num3);
        mTVNum4 = view.findViewById(R.id.tx_num4);
        mTVNum5 = view.findViewById(R.id.tx_num5);
        mTVNum6 = view.findViewById(R.id.tx_num6);
        mTVNum7 = view.findViewById(R.id.tx_num7);
        mTVNum8 = view.findViewById(R.id.tx_num8);
        mTVNum9 = view.findViewById(R.id.tx_num9);
        mTVNumSure = view.findViewById(R.id.tx_num_sure);
        mTVNumCircle = view.findViewById(R.id.tx_num_circle);
        mTVNumClear = view.findViewById(R.id.tx_num_clear);
        mTVNumSure.setOnClickListener(this);
        mTVNumCircle.setOnClickListener(this);
        mTVNumClear.setOnClickListener(this);
        mTVNum0.setOnClickListener(this);
        mTVNum1.setOnClickListener(this);
        mTVNum2.setOnClickListener(this);
        mTVNum3.setOnClickListener(this);
        mTVNum4.setOnClickListener(this);
        mTVNum5.setOnClickListener(this);
        mTVNum6.setOnClickListener(this);
        mTVNum7.setOnClickListener(this);
        mTVNum8.setOnClickListener(this);
        mTVNum9.setOnClickListener(this);
        mContext = this.getActivity();
        dailyMenusAdapter = new DailyMenusAdapter(mContext, lists);
        gv.setAdapter(dailyMenusAdapter);
        EventBus.getDefault().register(this);//EventBus.getDefault()用于得到一个EventBus实例
        //菜品数据刷新

        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
        if (dailyMenusAdapter != null) {
            dailyMenusAdapter.notifyDataSetChanged();
            //myAdapter.notifyDataSetChanged();
        }
    }

    //点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_setting_priced:
                if (canInputCash) {
                    tx_priced.setText("定价菜");
                    linearLayout1.setVisibility(View.VISIBLE);
                    linearLayout2.setVisibility(View.GONE);
                    canInputCash = false;
                } else {
                    inputCash = "";
                    tx_priced.setText("菜单点选");
                    linearLayout1.setVisibility(v.GONE);
                    linearLayout2.setVisibility(View.VISIBLE);
                    linearLayout1.setVisibility(View.GONE);
                    canInputCash = true;
                }
                break;
            case R.id.main_setting_maintain:
                //跳转页面
                Intent intent2 = new Intent(mContext, MenuTendingMainActivity2.class);
                intent2.putExtra("list", (Serializable) lists);
                startActivity(intent2);
                break;
            case R.id.main_setting_report_form:
                break;
            case R.id.tx_num_sure:
                inputCash = "";
                mETCash.setText(inputCash);
                break;

            //清除
            case R.id.tx_num_clear:
                inputCash = "";
                mETCash.setText(inputCash);
                break;
            //小数点
            case R.id.tx_num_circle:
                inputCash = mETCash.getText().toString();
                if (StringUtils.isEmpty(inputCash)) {
                    Toast.makeText(mContext, "输入错误，首位不能是小数点", Toast.LENGTH_SHORT).show();
                } else if (inputCash.contains(".")) {

                    Toast.makeText(mContext, "输入错误,小数点只能输入一次", Toast.LENGTH_SHORT).show();
                } else {
                    mETCash.setText(inputCash + ".");
                }
                break;
            case R.id.tx_num0:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "0";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num1:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "1";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num2:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "2";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num3:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "3";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num4:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "4";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num5:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "5";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num6:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "6";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num7:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "7";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num8:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "8";
                mETCash.setText(inputCash);
                break;
            case R.id.tx_num9:
                inputCash = mETCash.getText().toString();
                inputCash = inputCash + "9";
                mETCash.setText(inputCash);
                break;

        }
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setEvent(MessageEvent messageEvent) {
        lists.clear();
        lists.addAll(messageEvent.getMessage());
        dailyMenusAdapter.notifyDataSetChanged();

    }

}